package com.zzty.txj.serviceImpl;


import com.zzty.txj.dao.QuestionMapper;
import com.zzty.txj.dao.SelfMappper;
import com.zzty.txj.entity.Question;
import com.zzty.txj.entity.SelfExam;
import com.zzty.txj.service.SelfService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SelfServiceImpl implements SelfService {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SelfServiceImpl.class);


    @Autowired
    SelfMappper selfMappper;

    @Autowired
    QuestionMapper questionMapper;

    public Integer insert(SelfExam selfExam) throws Exception {
        Integer count=selfMappper.insertQuestionId(selfExam);

        return count;
    }

    public String getQuestionIdByDate(String date) {
        String result = "";
        String questionId=selfMappper.getQuestionIdByDate(date);
        System.out.println("questionId==="+questionId);
        logger.info("questionId=="+questionId);
        if (questionId == null) {
            result = "success";
        }
        else {
            result = "exists";
        }
        return result;
    }

    @Override
    public Map<String, List<Question>> getContentByDate(String date) {
        Map<String,List<Question>>map = new HashMap<>();
        List<Question>questions = new ArrayList<>();
        String result = selfMappper.getQuestionIdByDate(date);
        System.out.println("result=="+result);
        if(result==null){
            map.put("result",null);
        }
        else{
        String questionId[] = result.split(",");
        System.out.println("questionId===="+questionId);
        for(String s:questionId){
            System.out.println("s=="+s);
            Integer c=Integer.valueOf(s);
            Question question = questionMapper.getContent(c);
            System.out.println("question==="+question);
          questions.add(question);
        }
        map.put("result",questions);
        }
        System.out.println(map.get("result"));
        return map;
    }

    @Override
    public Integer getSelfExamId(String date) {
        Integer selfExamId = selfMappper.getSelfExamId(date);
        System.out.println("selfExamId===="+selfExamId);
        return selfExamId;
    }
//
//    public List getSelfExamToTable() {
//        List<SelfExam> selfExams=selfMappper.getSelfExamToTable();
//        return selfExams;
//    }
//
//    public boolean checkDate(String date) {
//        boolean result=true;
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        List<SelfExam> selfExams=selfMappper.getSelfExamToTable();
//        for (int i=0; i < selfExams.size(); i++) {
//            if (date.equals(sdf.format(selfExams.get(i).getDate()))) {
//                result=false;
//            }
//        }
//
//        return result;
//    }
}
